package com.learn.micro_mall.domain.entity.member;

import com.learn.micro_mall.domain.vo.member.LevelPointConfigurationVO;
import com.learn.micro_mall.domain.vo.member.MemberLevelConfigurationId;
import com.learn.micro_mall.domain.vo.member.MemberLevelEntityId;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

@Data
public class MemberLevelConfigurationEntity {

    private MemberLevelConfigurationId memberLevelConfigurationId;

    /**
     * 会员等级配置
     */
    private List<MemberLevelEntity> memberLevelEntityList;

    /**
     * 等级值点获取配置
     */
    private LevelPointConfigurationVO levelPointConfigurationVO;

    public void valid() throws Exception {
        for (MemberLevelEntity memberLevelEntity : memberLevelEntityList) {
            memberLevelEntity.valid();
        }
        List<Integer> levelNums = memberLevelEntityList.stream().map(MemberLevelEntity::getMemberLevelEntityId).map(MemberLevelEntityId::getLevel).sorted().collect(Collectors.toList());
        Integer totalLevelNum = 0;
        Integer before = 1;
        for (Integer levelNum : levelNums) {
            totalLevelNum += levelNum - before;
            before = levelNum;
        }
        if(totalLevelNum != 0){
            throw new Exception("等级值不合法");
        }
        levelPointConfigurationVO.valid();
    }
}
